﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Billing.WindowsBase;
using Infragistics.Win.UltraWinTree;
using Billing;
using Billing.DataObjects;
using System.Data.Linq;
using System.Data.SqlClient;

namespace Billing.Windows.Shell
{
    public partial class DataRightsForm_Old : FormBase
    {

        private Infragistics.Win.UltraWinTree.UltraTree trParentData;
        private System.Windows.Forms.Panel panel1;
        private Infragistics.Win.Misc.UltraButton ubUpdate;
        private Infragistics.Win.UltraWinTree.UltraTree trChildData;
        private Infragistics.Win.Misc.UltraButton ubRefresh;
        private System.Windows.Forms.Splitter splitter1;
        private Infragistics.Win.Misc.UltraButton ubExit;
        private TextBox txtSearch;
        private Infragistics.Win.Misc.UltraButton btnSearch;
        private System.ComponentModel.IContainer components;
        private ShellDataContext mShellDataContext = new ShellDataContext();


        public DataRightsForm_Old()
            : base(true)
        {
            InitializeComponent();
        }

        public DataRightsForm_Old(string x)
            : base(true)
        {
            InitializeComponent();
        }

        #region LoadDataTrees

        bool LoadedParentTypes = false;
        bool LoadedParents = false;
        //bool LoadedChildTypes=false;
        bool LoadedChilds = false;
        bool LoadedChildRights = false;
        Guid LoadedParentTypeGUID = Guid.Empty;
        Guid LoadedParentGUID = Guid.Empty;

        private void LoadParentDataTree()
        {
            this.trParentData.Nodes.Clear();
            this.LoadedParentTypes = false;
            this.LoadedParents = false;

            this.trChildData.Nodes.Clear();
            this.LoadedParentTypeGUID = Guid.Empty;
            this.LoadedParentGUID = Guid.Empty;
            //this.LoadedChildTypes=false;
            this.LoadedChilds = false;
            this.LoadedChildRights = false;
            //

            UltraTreeNode tn;
            DataValueBase dv;
            IDataReader dr;

            if (!this.LoadedParentTypes)
            {
                foreach (ParentTypesSelectResult item in mShellDataContext.ParentTypesSelect())
                {
                    dv = new DataValueBase(item.GUID, "", item.NAME, item.TABLE, item.VIEW);
                    tn = this.trParentData.Nodes.Add(dv.NAME);
                    tn.Tag = dv.Clone();
                }
                this.LoadedParentTypes = true;
            }
            if (!this.LoadedParents)
            {
                foreach (UltraTreeNode nd in this.trParentData.Nodes)
                {
                    foreach (ParentTypesSelectResult item in mShellDataContext.ExecuteQuery<ParentTypesSelectResult>(((DataValueBase)nd.Tag).SQLSelect()))
                    {
                        dv = new DataValueBase(item.GUID, item.KEY, item.NAME, item.TABLE, item.VIEW);
                        tn = nd.Nodes.Add(dv.GUID.ToString(), dv.NAME);
                        tn.Tag = dv.Clone();
                    }
                }
                this.LoadedParents = true;
            }
        }
        private void LoadChildDataTree()
        {
            UltraTreeNode an = this.trParentData.ActiveNode;
            UltraTreeNode tn;
            DataValueBase dv;
            IDataReader dr;

            if (this.LoadedParents && (an != null))
            {
                if (this.LoadedParentTypeGUID != ((DataValueBase)an.RootNode.Tag).GUID)
                {
                    this.trChildData.Nodes.Clear();

                    //this.LoadedChildTypes=false;

                    this.LoadedParentTypeGUID = ((DataValueBase)an.RootNode.Tag).GUID;
                    foreach (GetChildsResult item in mShellDataContext.GetChilds(this.LoadedParentTypeGUID))
                    {
                        dv = new DataValueBase(item.GUID, "", item.NAME, item.TABLE, item.VIEW);
                        tn = this.trChildData.Nodes.Add(dv.NAME);
                        tn.Tag = dv.Clone();
                        tn.Override.NodeStyle = Infragistics.Win.UltraWinTree.NodeStyle.Standard;
                    }
                    //dr.Close();
                    //this.LoadedChildTypes=true;
                    this.LoadedChilds = false;
                    this.LoadedChildRights = false;

                }
                if (!this.LoadedChilds)
                {
                    foreach (UltraTreeNode nd in this.trChildData.Nodes)
                    {
                        //dr = this.DBprovider.getDataReader(((DataValueBase)nd.Tag).SQLSelect());
                        //while (dr.Read())
                        //{
                        //    dv = new DataValueBase(dr.GetGuid(0), dr.GetString(1), dr.GetString(2), ((DataValueBase)nd.Tag).TABLE, ((DataValueBase)nd.Tag).VIEW);
                        //    tn = nd.Nodes.Add(dv.GUID.ToString(), dv.NAME);
                        //    tn.Tag = dv.Clone();
                        //}
                        //dr.Close();

                        foreach (ParentTypesSelectResult item in mShellDataContext.ExecuteQuery<ParentTypesSelectResult>(((DataValueBase)nd.Tag).SQLSelect()))
                        {
                            dv = new DataValueBase(item.GUID, item.KEY, item.NAME, item.TABLE, item.VIEW);
                            tn = nd.Nodes.Add(dv.GUID.ToString(), dv.NAME);
                            tn.Tag = dv.Clone();
                        }
                    }
                    this.LoadedChilds = true;
                    this.LoadedChildRights = false;
                }
                if (an.IsRootLevelNode)
                {
                    if (this.LoadedChildRights)
                    {
                        foreach (UltraTreeNode nd in this.trChildData.Nodes)
                        {
                            foreach (UltraTreeNode nd1 in nd.Nodes)
                            {
                                nd1.CheckedState = System.Windows.Forms.CheckState.Unchecked;
                            }
                        }
                        this.LoadedChildRights = false;
                    }
                }
                else
                {
                    if (this.LoadedParentGUID != ((DataValueBase)an.Tag).GUID)
                    {
                        if (this.LoadedChildRights)
                        {
                            foreach (UltraTreeNode nd in this.trChildData.Nodes)
                            {
                                foreach (UltraTreeNode nd1 in nd.Nodes)
                                {
                                    nd1.CheckedState = System.Windows.Forms.CheckState.Unchecked;
                                }
                            }
                            this.LoadedChildRights = false;
                        }
                        this.LoadedParentGUID = ((DataValueBase)an.Tag).GUID;
                    }
                    if (!this.LoadedChildRights)
                    {
                        foreach (UltraTreeNode nd in this.trChildData.Nodes)
                        {
                            foreach (UltraTreeNode nd1 in nd.Nodes)
                            {
                                nd1.CheckedState = System.Windows.Forms.CheckState.Unchecked;
                            }
                            nd.CheckedState = System.Windows.Forms.CheckState.Unchecked;
                            dv = (DataValueBase)nd.Tag;
                            //dr = this.DBprovider.getDataReader("SELECT DataGUID FROM dbo.getDataRightPC('"
                            //    + this.LoadedParentTypeGUID.ToString() + "','" + this.LoadedParentGUID.ToString() + "','" + dv.GUID.ToString() + "')");

                            foreach( getDataRightPCResult item in mShellDataContext.getDataRightPC(this.LoadedParentTypeGUID, this.LoadedParentGUID, dv.GUID))
                            {
                                try
                                {
                                    nd.Nodes[item.DataGUID.ToString()].CheckedState = System.Windows.Forms.CheckState.Checked;
                                }
                                catch (System.Exception ex)
                                {
                                    // MessageBox.Show(ex.Message);
                                }
                            }
                            //dr.Close();
                        }
                        this.LoadedChildRights = true;
                    }
                }
            }
        }
        #endregion

        private void ubUpdate_Click(object sender, System.EventArgs e)
        {
            DataValueBase dv;
            foreach (UltraTreeNode nd in this.trChildData.Nodes)
            {
                foreach (UltraTreeNode nd1 in nd.Nodes)
                {
                    dv = (DataValueBase)nd1.Tag;
                    if (dv.Updated)
                    {
                        mShellDataContext.ManageDataRight((Guid?)this.LoadedParentTypeGUID, (Guid?)this.LoadedParentGUID, (Guid?)((DataValueBase)nd.Tag).GUID, (Guid?)dv.GUID, (bool?)(nd1.CheckedState == System.Windows.Forms.CheckState.Checked));
                        dv.Updated = false;
                    }
                }
            }
        }

        private void frmDataRights_Load(object sender, System.EventArgs e)
        { LoadParentDataTree(); }

        private void trParentData_AfterSelect(object sender, Infragistics.Win.UltraWinTree.SelectEventArgs e)
        { LoadChildDataTree(); }

        private void trChildData_AfterCheck(object sender, Infragistics.Win.UltraWinTree.NodeEventArgs e)
        { ((DataValueBase)e.TreeNode.Tag).Updated = true; }

        private void trChildData_BeforeCheck(object sender, Infragistics.Win.UltraWinTree.BeforeCheckEventArgs e)
        { if (this.trParentData.ActiveNode.IsRootLevelNode && (e.TreeNode.CheckedState != System.Windows.Forms.CheckState.Checked)) e.Cancel = true; }

        private void ubRefresh_Click(object sender, System.EventArgs e)
        { this.LoadParentDataTree(); }

        private void ubExit_Click(object sender, System.EventArgs e)
        { this.Close(); }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            int StartIndex = 0;

            if (this.trParentData.SelectedNodes.Count > 0 && this.trParentData.SelectedNodes[0].Parent.Index == 1)
                StartIndex = this.trParentData.SelectedNodes[0].Index + 1;

            for (int i = StartIndex; i < this.trParentData.Nodes[1].Nodes.Count; i++)
            {
                if (this.trParentData.Nodes[1].Nodes[i].ToString().Contains(this.txtSearch.Text.ToString()))
                {
                    this.trParentData.Nodes[1].Nodes[i].Selected = true;
                    break;
                }
            }

        }

        private void txtSearch_TextChanged(object sender, EventArgs e)
        {
            if (this.trParentData.SelectedNodes.Count > 0)
            {
                this.trParentData.SelectedNodes[0].Selected = false;
            }
        }

        private void txtSearch_Enter(object sender, EventArgs e)
        {
            InputLanguage.CurrentInputLanguage = InputLanguage.FromCulture(new System.Globalization.CultureInfo("ka-ge"));
        }
    }
}
